DWORD Work_Save2JPG()
{
    int videoStream = -1;
    AVCodecContext *pCodecCtx;
    AVFormatContext *pFormatCtx;
    AVCodec *pCodec;
    AVFrame *pFrame, *pFrameRGB;
    struct SwsContext *pSwsCtx;
    const char *filename = "bigbuckbunny_480x272.h264";
    AVPacket packet;
    int frameFinished;
    int PictureSize;
    uint8_t *outBuff;

    //注册编解码器
    av_register_all();
    // 初始化网络模块
    avformat_network_init();
    // 分配AVFormatContext
    pFormatCtx = avformat_alloc_context();

    //打开视频文件
    if( avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0 ) {
        printf ("av open input file failed!\n");
        exit (1);
    }

    //获取流信息
    if( avformat_find_stream_info(pFormatCtx, NULL) < 0 ) {
        printf ("av find stream info failed!\n");
        exit (1);
    }
    //获取视频流
    for( int i = 0; i < pFormatCtx->nb_streams; i++ ) {
        if ( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
            videoStream = i;
            break;
        }
    }
    if( videoStream == -1 ) {
        printf ("find video stream failed!\n");
        exit (1);
    }

    // 寻找解码器
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if( pCodec == NULL ) {
        printf ("avcode find decoder failed!\n");
        exit (1);
    }

    //打开解码器
    if( avcodec_open2(pCodecCtx, pCodec, NULL) < 0 ) {
        printf ("avcode open failed!\n");
        exit (1);
    }

    //为每帧图像分配内存
    pFrame = avcodec_alloc_frame();
    pFrameRGB = avcodec_alloc_frame();
    if( (pFrame == NULL) || (pFrameRGB == NULL) ) {
        printf("avcodec alloc frame failed!\n");
        exit (1);
    }

    // 确定图片尺寸
    PictureSize = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);
    outBuff = (uint8_t*)av_malloc(PictureSize);
    if( outBuff == NULL ) {
        printf("av malloc failed!\n");
        exit(1);
    }
    avpicture_fill((AVPicture *)pFrameRGB, outBuff, PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);

    //设置图像转换上下文
    pSwsCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
        pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUVJ420P,
        SWS_BICUBIC, NULL, NULL, NULL);

    int i = 0;
    while( av_read_frame(pFormatCtx, &packet) >= 0 ) {
        if( packet.stream_index == videoStream ) {
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

            if( frameFinished ) {
                // 保存为jpeg时不需要反转图像
                static bool b1 = true;
                if( b1 ) {
                    MyWriteJPEG(pFrame, pCodecCtx->width, pCodecCtx->height, i ++);

                    b1 = false;
                }

                //SaveAsBMP(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i ++, 24);
            }
        } else {
            int a=2;
            int b=a;
        }

        av_free_packet(&packet);
    }

    sws_freeContext(pSwsCtx);

    av_free(pFrame);
    av_free(pFrameRGB);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    return 0;
}